package com.myacg.editor.common.unit.list;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.swing.JPanel;
/**
 * 流式布局面板提供一些基本的事件
 * @author Administrator
 *
 */
import javax.swing.JScrollPane;

import com.myacg.editor.common.unit.list.item.FlowListItem;

public class FlowListPanel<T> extends JPanel{
	private JScrollPane  jScrollPane;
	private JPanel  contentPanel;
	private Class<? extends FlowListItem<T>>   dataItemClass; //item控件对应的类型
	//对应的数据结构 便于增删改查
	private Map<String , FlowListItem<T>> itemList;
	//剥离出去的事件触发机制
	private ListEvent  listEvent;
	//list控件里要保存选择的状态
	private String selectId;
	public FlowListPanel(Class<? extends FlowListItem<T>>   dataItemClass ,ListEvent<T>  listEvent){
		this.dataItemClass = dataItemClass;
		itemList = new HashMap<>();
		//流式布局
		contentPanel = new JPanel(new VerticalFlowLayout(VerticalFlowLayout.TOP,1,1,true,false));
		contentPanel.setOpaque(false);
		//加入阴影和滚动面板
		jScrollPane = new JScrollPane(contentPanel);
		jScrollPane.setOpaque(false);
		this.setLayout(new BorderLayout());
		this.add(jScrollPane,BorderLayout.CENTER);
		this.listEvent =  listEvent;
	}
	
	public FlowListPanel(Class<? extends FlowListItem<T>>   dataItemClass ){
		 this(dataItemClass, null);
	}
	
	/**
	 * 添加一个Item
	 * @param id
	 * @param data
	 */
	public void addItem(String id, T data) {
		FlowListItem<T>  flowListItemPanel = null ; 
		try {
			 //构造一个对象
		     flowListItemPanel	 = dataItemClass.getConstructor(String.class,data.getClass()).newInstance(id,data);
		     flowListItemPanel.setParent(this); //设置父组件
		     flowListItemPanel.setListEvent(this.listEvent); //设置后置的回掉函数对象
		     this.itemList.put(id, flowListItemPanel);
		     //加入对应的UI对象
		     this.contentPanel.add(flowListItemPanel);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 添加一个id随机的item对象
	 * @param data
	 */
	public void addItem(T data) {
		this.addItem(UUID.randomUUID().toString().toLowerCase(),data);
	}
	/**
	 * 从列表里移除对应的item
	 * @param id
	 */
	public FlowListItem<T>  removeItem(String id){
		 FlowListItem<T> item = this.itemList.remove(id);
	     this.contentPanel.remove(item);
	    if( this.selectId == id){
	    	this.selectId = null;
	    }
	     return item;
	}

	public String getSelectId() {
		return selectId;
	}

	/**
	 * 设置选择的id 默认情况下 项组件会自动触发
	 * @param selectId
	 */
	public void setSelectId(String selectId){
		if(this.selectId != null){
			this.itemList.get(this.selectId).setSelected(false);
			if(this.selectId == selectId){
				this.selectId = null;
			}else{ 
				 this.itemList.get(selectId).setSelected(true);
				 this.selectId = selectId;
			}
		}else {
			this.selectId = selectId;
			this.itemList.get(selectId).setSelected(true);
		}
	}

	public ListEvent getListEvent() {
		return listEvent;
	}
	
	 
	public void setListEvent(ListEvent listEvent) {
		this.listEvent = listEvent;
		for (String key : itemList.keySet()) {
			itemList.get(key).setListEvent(this.listEvent);
		}
	}

	public  FlowListItem<T>  getItem(String id) {
		return this.itemList.get(id);
	}

	public Map<String, FlowListItem<T>> getItemList() {
		return itemList;
	}

	
}
